package com.example.administrator.myfragment;


import android.app.Activity;
import android.app.ListFragment;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MessageFragment extends ListFragment {
     private  TextView tv;
    private ListView listView;
    private SimpleAdapter adapter;
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    public MessageFragment() {
        // Required empty public constructor
    }

    @Override
    public  void onActivityCreated(Bundle saveInstanceSate)
    {
        super.onActivityCreated(saveInstanceSate);
        listView= (ListView)getActivity().findViewById(android.R.id.list);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                HashMap<String,Object> views=(HashMap<String,Object>)adapterView.getItemAtPosition(i);
                Toast.makeText(getActivity(), "点击列表有值了" +views.get("title"), Toast.LENGTH_LONG).show();
                //打开新activity 并传递值
                Intent intent =new Intent(getActivity().getApplicationContext(),ThirdfActivity.class);
                intent.putExtra("title",views.get("title").toString());
                getActivity().startActivity(intent);

            }
        });
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.message_layout,container,false);
        return  view;
    }
    @Override
    public  void  onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        adapter=new SimpleAdapter(getActivity(),getData(),R.layout.vlist,new String[]{"title","info","img"},
                new int[]{R.id.title,R.id.info,R.id.img});
        setListAdapter(adapter);

        //Toast.makeText(getActivity(),"1",Toast.LENGTH_SHORT).show();
    }

    private List<Map<String, Object>> getData() {

        for(int index=1;index<50;index++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", "G1"+index);
            map.put("info", "google "+index);
            map.put("img", R.drawable.ic_launcher);
            list.add(map);
        }
        return list;
    }

}

